package yawning.mew.bear;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;

public class SwipeAction implements SimulationAction {
	BearThreatSimulator mState;
	double mCooldownExpireTime = 0.0;

	SwipeAction(BearThreatSimulator state) {
		mState = state;
	}

	@Override
	public double perform() {
		int cost = mState.mModel.swipeCost;

		if (!mState.hasRage(cost) && !mState.mOoCEv.isUp()) return SimulationAction.ACTION_FAIL;
		if (mCooldownExpireTime > mState.mSimEngine.getSimTime()) return SimulationAction.ACTION_FAIL;

		AttackResult result = mState.mModel.getFrontalYellowResult();

		if (result == AttackResult.MISS) {
			mNrSwipeMiss++;

			if (mState.mIsLogging) mState.mLog.log("Swipe: MISS\n");
		} else if (result == AttackResult.DODGE) {
			mNrSwipeDodge++;

			if (mState.mIsLogging) mState.mLog.log("Swipe: DODGE\n");
		} else if (result == AttackResult.PARRY) {
			mNrSwipeParry++;

			if (mState.mIsLogging) mState.mLog.log("Swipe: PARRY\n");
		} else {
			double damage =  mState.mModel.mLevelDep.getSwipeBaseDmg() + mState.mModel.getAttackPower() * mState.mModel.mLevelDep.getSwipeCoeff();

			if (result == AttackResult.CRIT) {
				mNrSwipeCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrSwipeHit++;
			}

			damage *= mState.getDirectDamageMult();
			mSwipeDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Swipe: %s Damage: %d\n", result, Math.round(damage));

			if (result == AttackResult.CRIT) {
				mState.mSavageDefenseEv.onTrigger();
				mState.mLotPEv.onTrigger();
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOWCRIT);
			} else {
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOW);
			}
		}

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.swipeCDDuration;

		mState.onRageLoss(cost);

		if (result == AttackResult.CRIT) mState.onPrimalFuryTrigger();

		return 1.5;
	}


	long mSwipeDamage = 0;
	int mNrSwipeMiss = 0;
	int mNrSwipeDodge = 0;
	int mNrSwipeParry = 0;
	int mNrSwipeCrit = 0;
	int mNrSwipeHit = 0;
}
